package BinaryTree;

//平衡二叉树: https://leetcode.cn/problems/balanced-binary-tree/description/
//时间复杂度: O(n)
public class BlanceTree {
	public static boolean blance;
	
	public int height(TreeNode root) {
		if(root==null) return 0;
		int lh=height(root.left); //左树高
		int rh=height(root.right);//右树高
	    if(Math.abs(lh-rh)>1) {   //计算高度差
	    	blance=false;
	    }
	    return 1+Math.max(lh,rh);
	}
	public boolean isBalanced(TreeNode root) {
		blance=true;   //由于blance为静态成员，因此每次都要置为初始值
		height(root);
		return blance;
	}
}
